package cn.dnui_xcz826.dnuifood_16110100826.controller;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import cn.dnui_xcz826.dnuifood_16110100826.R;
import cn.dnui_xcz826.dnuifood_16110100826.bean.ResultBean;
import cn.dnui_xcz826.dnuifood_16110100826.controller.BaseActivity;
import cn.dnui_xcz826.dnuifood_16110100826.listener.onRetrofitListener;
import cn.dnui_xcz826.dnuifood_16110100826.model.UserModel;

public class RegisterActivity extends BaseActivity implements View.OnClickListener,onRetrofitListener<ResultBean> {
    private Intent intent;
    private Button button,button2;
    private EditText etname,etpass,etqueren,etnumber,etadress,etcomment;
    private UserModel userModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    void initViews() {
        setLayout(R.layout.activity_register);
        button=(Button)findViewById(R.id.button);
        button2=(Button)findViewById(R.id.button2);
        etname=(EditText)findViewById(R.id.usernamere);
        etpass=(EditText)findViewById(R.id.userpass);
        etqueren=(EditText)findViewById(R.id.passyes);
        etnumber=(EditText)findViewById(R.id.userphone);
        etadress=(EditText)findViewById(R.id.usermail);
        etcomment=(EditText)findViewById(R.id.usercomment);

    }

    @Override
    void initEvents() {
        button.setOnClickListener(this);
        //处理返回按钮
        button2.setOnClickListener(this);

    }

    @Override
    void initDatas() {

    }

    @Override
    public void onSuccess(ResultBean object, int flag) {
        if (object.getSuccess().equals("1")){
            intent=new Intent();
            intent.putExtra("user",etname.getText().toString());
            setResult(RESULT_OK,intent);
            Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
            finish();
        }
        else {
            Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                if (etpass.getText().toString().equals(etqueren.getText().toString())){
                    userModel=new UserModel();
                    userModel.register(etname.getText().toString(),
                            etpass.getText().toString(),
                            etnumber.getText().toString(),
                            etadress.getText().toString(),
                            etcomment.getText().toString(),
                            this);

                }
                else {
                    Toast.makeText(RegisterActivity.this,"请确认两次密码相同",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.button2:
                intent=new Intent();
                intent.putExtra("user",etname.getText().toString());
                setResult(RESULT_OK,intent);
                finish();
                break;

        }

    }



    @Override
    public void onFailure(String msg) {
        Toast.makeText(RegisterActivity.this,"网络发生错误",Toast.LENGTH_SHORT).show();
    }
}
